/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers.Forms
{
	/// <summary>
	/// Represents information about a View in a Groove Forms tool.
	/// </summary>
	public class View : GrooveWebServicesV12HelpersBase
	{
		private GrooveWebServicesV12Helpers.Forms.FormsTool m_FormsTool;
		
		private GrooveFormsWebService.FormsView m_View;

		public View(GrooveWebServicesV12Helpers.Forms.FormsTool i_FormsTool, GrooveWebServicesV12Helpers.GrooveFormsWebService.FormsView i_View)
			: base(i_View.Name, i_View.URI, i_FormsTool.IdentityURL, i_FormsTool.Context)
		{
			m_FormsTool = i_FormsTool;
			m_View = i_View;
		}


		/// <summary>
		/// The FormsTool which contains this View.
		/// </summary>
		public GrooveWebServicesV12Helpers.Forms.FormsTool FormsTool
		{
			get
			{
				return m_FormsTool;
			}
		}

		/// <summary>
		/// The ID of this View.
		/// </summary>
		public string ID
		{
			get
			{
				return m_View.ID;
			}
		}

		/// <summary>
		/// The DateTime of the last modified record in the View.
		/// </summary>
		public System.DateTime LastModifiedRecordDateTime
		{
			get
			{
				return m_View.RecordsLastModifiedTimeDate;
			}
		}

		/// <summary>
		/// The name of the field which is the default sort in the View.
		/// </summary>
		public string DefaultSortPrimaryKeyName
		{
			get
			{
				return m_View.DefaultSortPrimaryKeyFieldName;
			}
		}

		/// <summary>
		/// Array of Columns in the View.
		/// </summary>
		public GrooveWebServicesV12Helpers.Forms.Column[] Columns
		{
			get
			{
				System.Collections.ArrayList ColumnsArrayList = new System.Collections.ArrayList();

                //  Create array list of view columns
				foreach (GrooveFormsWebService.ViewColumn ViewColumn in m_View.ViewColumnList)
				{
					GrooveWebServicesV12Helpers.Forms.Column NewColumn = new GrooveWebServicesV12Helpers.Forms.Column
						(
						this,
						ViewColumn
						);

					ColumnsArrayList.Add(NewColumn);
				}

				//  Return array from array list
				return (GrooveWebServicesV12Helpers.Forms.Column[])ColumnsArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.Forms.Column));
			}
		}

		#region GrooveWebServicesV12HelpersBase Implementation

		public override string BindableURL
		{
			get
			{
				if (System.String.IsNullOrEmpty(m_BindableURL))
					m_BindableURL = m_FormsTool.BindableURL + "/View#" + this.ID;

				return m_BindableURL;
			}
		}

		#endregion

	}
}
